/*
 * @lc app=leetcode.cn id=155 lang=javascript
 *
 * [155] 最小栈
 */

// @lc code=start

var MinStack = function () {
  this.items = [];
};

MinStack.prototype.size = function () {
  return this.items.length;
};

MinStack.prototype.isEmpty = function () {
  return this.size() === 0;
};

/**
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function (val) {
  if (this.isEmpty()) {
    this.items.push({
      value: val,
      min: val,
    });
  } else {
    this.items.push({
      value: val,
      min: Math.min(this.getMin(), val),
    });
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function () {
  return this.items.pop();
};

/**
 * @return {number}
 */
MinStack.prototype.top = function () {
  if (this.isEmpty()) {
    return undefined;
  }
  return this.items[this.size() - 1].value;
};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function () {
  if (this.isEmpty()) {
    return undefined;
  }
  return this.items[this.size() - 1].min;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */
// @lc code=end
